<?php
include_once 'gift_cert/lib/Common.php';
include_once 'gift_cert/lib/Constant.php';
class ImageText
{
	/**
	 * create image stream
	 *
	 * @param array $array
	 * @return image
	 */
	public static function createImage($array)
	{
		$idTpl = substr($array['template'],3);

		// Create the image
		$im = @imagecreatefromjpeg(SYS_PATH.Constant::IMG_PATH."/tpl".$idTpl.".jpg");
				
		// The text to draw
		$textArr[0] = $array['code'];
		$textArr[1] = "$".$array['amount'];
		$textArr[2] = $array['from'];
		$textArr[3] = $array['to'];
		$textArr[4] = $array['message'];

		//load config file
		$xmlFile = SYS_PATH."gift_cert/data/config/template.xml";
		if (file_exists($xmlFile)) {
			$xml = simplexml_load_file($xmlFile);
		}

		$result = $xml->xpath("//template[@id=\"".$idTpl."\"]");

		for( $i=0; $i<count($result[0]->item) ;$i++ )
		{
			self::imgText($result[0]->item[$i], $textArr[$i], $im);
		}
		return $im;
	}
	
	/**
	 * create image gift certificate
	 *
	 * @param array $array
	 * @return string
	 */
	public static function createGiftCert($array)
	{
		$im = self::createImage($array);
		$outImg = SYS_PATH.Constant::IMG_PATH."/output/gift_cert_".$array['code'].".jpg";
		imagejpeg($im, $outImg);
		imagedestroy($im);
		return Constant::IMG_PATH."/output/gift_cert_".$array['code'].".jpg";
	}
	
	public static function imgText($item, $text, $im)
	{	
		//get text color from config
		if( isset($item['color']) == TRUE )
		{
			$colors = split(",",$item['color']);
			$color = imagecolorallocate($im, $colors[0], $colors[1], $colors[2]);
		}
		else
		{
			$color = imagecolorallocate($im, 0, 0, 0);
		}
		
		//get font from config
		if( isset($item['font']) == TRUE )
		{
			$font = SYS_PATH."gift_cert/data/font/".$item['font'];
			if( file_exists($font) == FALSE)
			{
				$font = SYS_PATH.'gift_cert/data/font/arial.ttf';
			}
		}
		else
		{
			$font = SYS_PATH.'gift_cert/data/font/arial.ttf';
		}
		
		//get text columns from config
		if(isset($item['cols']) == FALSE)
		{
			imagettftext($im, floatval($item['size']), 0, intval($item['x']), 
									intval($item['y']), $color, $font, $text);
			return;
		}
		$cols = intval($item['cols']);
		
		//get rows
		$rows = isset($item['rows']) == TRUE ? $item['rows'] : 1;
		
		//get size
		$size = floatval($item['size']);
		
		//get space
		$space = isset($item['space']) == TRUE ? $item['space'] : $size * 2;
		
		//get text length
		$length = mb_strlen($text);
		
		$arrText = Common::splitByLength($text,$cols);
			
		$count = count($arrText)<$rows?count($arrText):$rows;
		for($i=0; $i< $count; $i++)
		{
			$yy = intval($item['y']) + $i * $space ;
			imagettftext($im, $size , 0, intval($item['x']), $yy, $color, $font, $arrText[$i]);
		}
	}
	
	/**
	 * clean images a month ago
	 *
	 */
	public static function cleanImage()
	{
		$imgDir = SYS_PATH.Constant::IMG_PATH."/output/";
		if ($handle = opendir($imgDir)) {
	    while (false !== ($file = readdir($handle))) {
	        if ($file != "." && $file != "..") {
	        	$mFile = date("m",filemtime($imgDir.$file));
				$mCur = date("m",time());
				if($mCur > $mFile)
				{
					unlink($imgDir.$file);
				}
	        }
	    }
		}
	    closedir($handle);
	}
}
?>